# Maintainer: Luis Martinez <luis.martinez@disroot.org>
# Contributor: Rafał Brzegowy <rafal.brzegowy@yahoo.com>

_realname=mfem
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.5
pkgrel=2
pkgdesc='Lightweight, general, scalable C++ library for finite element methods (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-msmpi"
	 "${MINGW_PACKAGE_PREFIX}-hypre"
	 "${MINGW_PACKAGE_PREFIX}-metis"
	 "${MINGW_PACKAGE_PREFIX}-lapack"
	 "${MINGW_PACKAGE_PREFIX}-suitesparse")
makedepends=("${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake")
license=('BSD')
url='https://github.com/mfem/mfem'
source=("${_realname}-${pkgver}.tar.gz::$url/archive/v$pkgver.tar.gz"
        "0000-fix.patch")
sha256sums=('ee0e640286ea370aaf5c973e8f08eb648fa650e3e30f296e68bfc10c3dcfa9b8'
            'dbca903bce62d3473ac7103718009e4198a1ea8afc55071d42c75a0ac197465d')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0000-fix.patch"
}

build() {
  #Static Build
  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_BUILD_TYPE='None' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=NO \
      -DMFEM_USE_MPI=YES \
      -DMFEM_USE_METIS=YES \
      -DMFEM_USE_LAPACK=YES \
      -DMFEM_USE_OPENMP=YES \
      -DMFEM_USE_MEMALLOC=YES \
      -DMFEM_USE_SUITESPARSE=YES \
      -DMFEM_USE_ZLIB=YES \
      -DMFEM_ENABLE_TESTING=NO \
      -DMFEM_ENABLE_EXAMPLES=NO \
      -DMFEM_ENABLE_MINIAPPS=NO \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
  
  #Shared Build
  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_BUILD_TYPE='None' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=YES \
      -DMFEM_USE_MPI=YES \
      -DMFEM_USE_METIS=YES \
      -DMFEM_USE_LAPACK=YES \
      -DMFEM_USE_OPENMP=YES \
      -DMFEM_USE_MEMALLOC=YES \
      -DMFEM_USE_SUITESPARSE=YES \
      -DMFEM_USE_ZLIB=YES \
      -DMFEM_ENABLE_TESTING=NO \
      -DMFEM_ENABLE_EXAMPLES=NO \
      -DMFEM_ENABLE_MINIAPPS=NO \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
  
}

package() {
  #Static Install
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR=${pkgdir} cmake --build . --target install

  #Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR=${pkgdir} cmake --build . --target install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/README.md ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/README
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/NOTICE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/NOTICE

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in $(find "${pkgdir}${MINGW_PREFIX}"/lib/cmake -type f); do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

}
